package com.jos.appium;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.android.AndroidDriver;

public class AndroidWechatFocus {

	private String titleName;

	public AndroidWechatFocus(String titleName) {
		super();
		this.titleName = titleName;
	}

	protected AndroidDriver<WebElement> getDriver() throws MalformedURLException {
		System.out.println("---getDriver");

		DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
		desiredCapabilities.setCapability("platformName", "Android");
		desiredCapabilities.setCapability("deviceName", "127.0.0.1:7555");
		desiredCapabilities.setCapability("platformVersion", "6.0.1");
		desiredCapabilities.setCapability("appPackage", "com.tencent.mm");
		desiredCapabilities.setCapability("appActivity", ".ui.LauncherUI");
		desiredCapabilities.setCapability("sessionOverride", true);
		desiredCapabilities.setCapability("noReset", true);
		desiredCapabilities.setCapability("fullReset", false);
		desiredCapabilities.setCapability("unicodeKeyboard", true);
		desiredCapabilities.setCapability("newCommandTimeout", "30000");
		desiredCapabilities.setCapability("showChromedriverLog", true);
		desiredCapabilities.setCapability("recreateChromeDriverSessions", "True");

		URL remoteUrl = new URL("http://localhost:4723/wd/hub");
		AndroidDriver<WebElement> driver = new AndroidDriver<WebElement>(remoteUrl, desiredCapabilities);

		driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
		return driver;
	}

	private WebElement iconSearchEl(AndroidDriver<WebElement> driver) throws InterruptedException {
		System.out.println("---iconSearchEl");

		List<WebElement> elementList = driver.findElements(By.id("com.tencent.mm:id/r_"));
		if (elementList == null || elementList.size() == 0) {
			throw new RuntimeException("not found element");
		}
		return elementList.get(0);
	}

	private WebElement textSearchEl(AndroidDriver<WebElement> driver) throws InterruptedException {
		System.out.println("---textSearchEl");

		List<WebElement> elementList = driver.findElements(By.id("com.tencent.mm:id/m7"));
		if (elementList == null || elementList.size() == 0) {
			throw new RuntimeException("not found element");
		}
		return elementList.get(0);
	}

	private WebElement itemSearchEl(AndroidDriver<WebElement> driver) throws InterruptedException {
		System.out.println("---itemSearchEl");

		List<WebElement> elementList = driver.findElements(By.id("com.tencent.mm:id/c75"));

		if (elementList == null || elementList.size() == 0) {
			throw new RuntimeException("not found element");
		}
		return elementList.get(0);
	}

	private WebElement resSearchEl(AndroidDriver<WebElement> driver) throws InterruptedException {
		System.out.println("---resSearchEl");

		List<WebElement> elementList = driver.findElements(By.xpath(
				"(//android.widget.FrameLayout[@content-desc=\"containerFrameLayout\"])[2]/android.widget.FrameLayout/android.webkit.WebView"));
		if (elementList == null || elementList.size() == 0) {
			throw new RuntimeException("not found element");
		}
		WebElement parent = elementList.get(0);
		Thread.sleep(1 * 1000);

		List<WebElement> elementList2 = parent
				.findElements(By.xpath("//android.view.View[contains(@content-desc,'" + this.titleName + "')]"));

		if (elementList == null || elementList.size() == 0) {
			throw new RuntimeException("not found element");
		}

		return elementList2.get(0);
	}

	private WebElement resFocusEl(AndroidDriver<WebElement> driver) throws InterruptedException {
		System.out.println("---resFocusEl");

		List<WebElement> elementList = driver.findElements(By.xpath(
				"//android.widget.FrameLayout[contains(@content-desc,'当前所在页面')]/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.ListView/android.view.ViewGroup[2]"));
		if (elementList == null || elementList.size() == 0) {
			throw new RuntimeException("not found element");
		}

		return elementList.get(0);
	}

	public void bussiness() throws Exception {
		AndroidDriver<WebElement> driver = getDriver();
		Thread.sleep(1 * 1000);

		WebElement iconSearchEl = iconSearchEl(driver);
		Thread.sleep(1 * 1000);
		iconSearchEl.click();
		Thread.sleep(3 * 1000);

		WebElement textSearchEl = textSearchEl(driver);
		Thread.sleep(1 * 1000);
		textSearchEl.click();
		Thread.sleep(3 * 1000);
		textSearchEl.sendKeys(this.titleName);

		WebElement itemSearchEl = itemSearchEl(driver);
		Thread.sleep(1 * 1000);
		itemSearchEl.click();
		Thread.sleep(3 * 1000);

		WebElement resSearchEl = resSearchEl(driver);
		Thread.sleep(1 * 1000);
		resSearchEl.click();
		Thread.sleep(3 * 1000);

		WebElement resFocusEl = resFocusEl(driver);
		Thread.sleep(1 * 1000);
		resFocusEl.click();
		Thread.sleep(3 * 1000);

	}

	public static void main(String[] args) {
		System.out.println("***main start");
		AndroidWechatFocus model = new AndroidWechatFocus("投资1加1");
		try {
			model.bussiness();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("***main end");
	}

}
